import urllib.request
import random

# 配置代理池
PROXY_POOL = [
    {'http': '202.108.22.5:80'},
    {'http': '180.97.34.35:80'},
    {'http': '112.80.248.73:80'},
]
# 使用处理器配置代理
BASE_URL = 'https://www.baidu.com/s?wd=ip'
# 伪装浏览器：
HEADER = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                  'AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/103.0.0.0 Safari/537.36',
}
RANDOM_IP = random.choice(PROXY_POOL)

# 创建请求对象
request = urllib.request.Request(url=BASE_URL, headers=HEADER)

# 获取处理器对象
handler = urllib.request.ProxyHandler(proxies=RANDOM_IP)
opener = urllib.request.build_opener(handler)
response = opener.open(request)

# 得到响应数据
html = response.read().decode('UTF-8')

# 写入磁盘
fp = open(file='ip查询.html', mode='w', encoding='UTF-8')
fp.write(html)
